#include "led.h"
#include <string.h>

//MCU相关的打印接口
#include "print.h"
//MCU相关的GPIO头文件
#include "gpio.h"

led_ctrl_t led_ctrl;

int32_t led_init(void )
{
	app_print("led_init..\n");
	memset(&led_ctrl, 0, sizeof(led_ctrl_t));

	return 0;
}

void led_on(uint32_t gpio, uint32_t ms)
{
	if (ms == 0) return;

	//假设给高电平打开led, write_gpio由MCU厂商提供
	write_gpio(gpio, 1);
	led_ctrl.num = gpio;
	led_ctrl.on_ms = ms;

	app_print("led_on, led_ctrl.num:%u, led_ctrl.on_ms:%u\n", led_ctrl.num, led_ctrl.on_ms);
}

void led_off(uint32_t gpio)
{
	//假设给高电平关闭led, write_gpio由MCU厂商提供
	write_gpio(gpio, 0);
	led_ctrl.on_ms = 0;
	led_ctrl.num = gpio;
	app_print("led_off. led_ctrl.num:%u, led_ctrl.on_ms:%u\n", led_ctrl.num, led_ctrl.on_ms);
}

uint32_t led_get_state(void )
{
	return read_gpio(led_ctrl.num);
}

//由应用放在一个定时器中定时调用，为了方便，这里假设定时器的定时间隔为1ms。
void lcd_ctrl(void )
{
	if (led_ctrl.on_ms) {
		led_ctrl.on_ms--;
		if (led_ctrl.on_ms == 0) {	
			led_off(led_ctrl.num);
			app_print("lcd_ctrl. led off, num: %u\n", led_ctrl.num);
		}
	}
}

